import _ from "lodash";
import { handleActions } from "redux-actions";
import { GET_ADDRESS,ADDRESS_SET_CUR_PAGE,ADDRESS_SET_EACH_PAGE} from "../types";


//创建初始值
const initAddress = {
    curPage: 1,
    eachPage: 3,
    total: 0,
    maxPage: 0,
    rows: [],
}

export const address = handleActions({
    [GET_ADDRESS]: (state, { payload }) => {
        const data = payload;
        return _.assign({}, state, data)
    },
 // 修改当前页
 [ADDRESS_SET_CUR_PAGE]: (state, { payload }) => {
    const { curPage } = payload;
    const $state = _.clone(state);
    $state.curPage = curPage;
    return $state;
  },
  // 修改每页显示条数
  [ADDRESS_SET_EACH_PAGE]: (state, { payload }) => {
    const { eachPage } = payload;
    const $state = _.clone(state);
    // // 因如果从每页10变为5条时，第一次渲染页面后，此时rows长度依然为10，但每页显示条数为5，所以浏览器会给出警告数据长度大于每页显示条数，此时如果rows数据赋值为空，则可以规避警告
    $state.rows = [];
    $state.eachPage = eachPage;
    return $state;
  },
}, initAddress)